package sugarcookie.commands {
	import sugarcookie.error.ApplicationError;
	
	public class DynamicCommand implements ICommand {
		private var _receiver:*;
		private var _method:String;
		private var _args:Array = [];
		
		public function DynamicCommand(receiver:*,method:String=null) {
			_receiver = receiver;
			_method = method;
		}
		
		public function set method(value:String):void {
			_method = value;
		}
		
		public function setMethodArgs(...args):void {
			_args = args;
		}
		
		public function execute():void {
			if (_method==null) throw new ApplicationError("The receiver's method has not been defined!");
			_receiver[_method].apply(null,_args);
		}
		
		public function clone():ICommand {
			return new DynamicCommand(_receiver,_method);
		}
	}
}